package railRoad.web.slaves;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import railRoad.Route;
import railRoad.Schedule;
import railRoad.Station;
import railRoad.web.slaves.interfaces.ISlaveServlet;

public class StationlistServlet implements ISlaveServlet {

	public void work(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		req.getSession(true).setAttribute("body", "/pages/select_station.jsp");

		int id_route;

		if (req.getParameterMap().containsKey("id_route")) {
			id_route = Integer.parseInt(req.getParameterValues("id_route")[0]);
		} else
			id_route = (Integer) req.getSession(true).getAttribute("id_route");

		Route route = Schedule.getInstance().getRouteById(id_route);
		int size = route.size();

		ArrayList<Station> stations = new ArrayList<Station>(size);

		for (int i = 0; i < size; i++)
			stations.add(route.getStationAt(i));

		req.getSession().setAttribute("id_route", id_route);
		req.getSession().setAttribute("buy", route.checkTicketonAvailaible());
		req.getSession().setAttribute("stations", stations);

		resp.sendRedirect("/index.jsp");

	}// work

}// StationlistServlet
